package com.ftdigital.primemovers_operators.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.AddDetail;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.ListEntity;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.ftdigital.primemovers_operators.util.view.RoundImageView;
import com.google.gson.JsonSyntaxException;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;

import java.util.List;

/**
 * 交易详情
 */
@EFragment(R.layout.fragment_trading_detail)
public class TradingDetailFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.edit_btn)
    ImageView editBtn;

    @ViewById(R.id.ratingBar)
    RatingBar ratingBar;

    @ViewById(R.id.headimg)
    RoundImageView headimg;

    @FragmentArg("order_id")
    int order_id;

    @ViewsById({R.id.username, R.id.company_detail, R.id.truck_type_content, R.id.licence_num_content,
            R.id.add_oil_station_content, R.id.add_oil_operator_content, R.id.work_no_content,
            R.id.add_oil_type_no_content, R.id.input_oil_L_hint_content, R.id.input_oil_market_price_content,
            R.id.input_oil_contract_price_content, R.id.input_oil_thisprice_price_content, R.id.instroduce_content})
    List<TextView> detailList;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_trading_detail));

        loadData();
    }

    private void loadData() {
        requestParams = "order_id="+order_id;
        requestUrl = RequestAddr.gasDeatail + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });

    }

    AddDetail addDetail;
    private void dealData(Object object) {
        try {
            addDetail = gson.fromJson(object.toString(), AddDetail.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        detailList.get(0).setText(addDetail.getData().getCompany_name());
        detailList.get(1).setText(addDetail.getData().getDescs());
        detailList.get(2).setText(addDetail.getData().getCar_type());
        detailList.get(3).setText(addDetail.getData().getCar_num());
        detailList.get(4).setText(addDetail.getData().getGasstation_name());//
        detailList.get(5).setText(addDetail.getData().getUser_name());//
        detailList.get(6).setText(addDetail.getData().getEmp_no());
        detailList.get(7).setText(addDetail.getData().getGas_type());
        detailList.get(8).setText(addDetail.getData().getQty());
        detailList.get(9).setText(addDetail.getData().getPrice());
        detailList.get(10).setText(addDetail.getData().getConvention_price());
        detailList.get(11).setText(addDetail.getData().getTotalprice());
        detailList.get(12).setText(addDetail.getData().getDetail());

        if (addDetail.getData().getTotal().equals("")){
            ratingBar.setProgress(0);
        }else {
            ratingBar.setProgress(Integer.parseInt(addDetail.getData().getTotal()));
        }
        Picasso.with(getActivity()).load(addDetail.getData().getIcon_url()).error(R.drawable.user_headimg).into(headimg);
    }

    @Click({R.id.back_btn, R.id.edit_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.edit_btn:
                break;
        }
    }


}
